package com.xy.tool.report;

import com.xy.tool.report.excel.ExcelTemplate;
import com.xy.tool.report.excel.MultiSheetExcelTemplate;
import com.xy.tool.report.view.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by zsp on 2019/6/15.
 */
public class ReportUtils {

    /**
     * 获取文本显示的行数
     *
     * @param text              文本
     * @param maxWordSize      每行显示的最大字数
     * @return
     */
    public static int getLineSize(String text, int maxWordSize) {
        int wordSize;
        if (text == null || (wordSize = text.length()) == 0) {
            return 0;
        }
        if (maxWordSize < 1) {
            maxWordSize = 1;
        }
        int lineSize = 0;
        for (int i = 0; i < wordSize; i += maxWordSize) {
            lineSize++;
        }
        return lineSize;
    }

    /**
     * 如果是空格，则返回一个替代文本
     * @param text          文本
     * @return
     */
    public static String replaceIfBlank(String text) {
        if (text == null || text.length() == 0 || text.trim().length() == 0) {
            return "-";
        }
        return text;
    }

    public static void main(String[] args) throws IOException {
        /*System.out.println("==========test getLineSize==========");
        System.out.println(getLineSize(null, 1));
        System.out.println(getLineSize("a", 0));
        System.out.println(getLineSize("a", 2));
        System.out.println(getLineSize("ab", 2));
        System.out.println(getLineSize("abc", 2));
        System.out.println(getLineSize("abcde", 2));
        System.out.println("==========test replaceIfBlank==========");
        System.out.println(replaceIfBlank(null));
        System.out.println(replaceIfBlank(""));
        System.out.println(replaceIfBlank(" "));
        System.out.println(replaceIfBlank("abc"));*/

        Cell cell = new Cell();
        // cell.setRowSpan(1);
        //cell.setSpan(1);
        cell.setValue("文件");
        //cell.setHorizontalAlignment(TextAlignment.Bottom);
        Cell cell1 = new Cell();
        cell1.setSpan(13);
        cell1.setHorizontalAlignment(TextAlignment.Left);
        cell1.setValue("记录编号：HM-ZB-JL-001");
        Style style = new Style();
        style.setFontSize((short) 9);
        style.setBold(false);
        cell1.setStyle(style);
        cell1.setVerticalAlignment(TextAlignment.Center);
        Cell cell2 = new Cell();
        cell2.setSpan(13);
        cell2.setHorizontalAlignment(TextAlignment.Center);
        cell2.setValue("霍尔姆斯（北京）诊断技术有限公司");
        cell2.setVerticalAlignment(TextAlignment.Center);
        Cell cell3 = new Cell();
        cell3.setSpan(13);
        cell3.setHorizontalAlignment(TextAlignment.Center);
        Style style1 = new Style();
        style1.setFontSize((short) 14);
        style1.setBold(true);
        cell3.setStyle(style1);
        cell3.setValue("文件使用、变更处理台账");
        cell3.setVerticalAlignment(TextAlignment.Center);
        Table table = new Table();
        table.setName("测试台账");
        //table.setStriped(true);
        //table.setStripeDirection(Direction.Horizontal);
        /*table.setStripeStartRowIndex(0);
        table.setStripeEndRowIndex(10);
        table.setStripeStartColIndex(0);
        table.setStripeEndColIndex(10);*/
        List<Row> headerList = table.getHeaderList();
        Row h1 = new Row();
        h1.addCell(cell1);
        Row h2 = new Row();
        h2.addCell(cell2);
        Row h3 = new Row();
        h3.addCell(cell3);
        headerList.add(h1);
        headerList.add(h2);
        headerList.add(h3);
        Row row1 = table.createRow();
        //row1.setBorder(10);
        //row1.setHeight(500);
        row1.addCell(cell);
        List<Table> list = new ArrayList();
        list.add(table);
        MultiSheetExcelTemplate multiSheetExcelTemplate = new MultiSheetExcelTemplate();
        SXSSFWorkbook workbook = multiSheetExcelTemplate.create(list, null);
        OutputStream out = new FileOutputStream("E:\\IDEA_Projects\\Xiaoyu\\xy-common\\xy-tool\\xy-tool-core\\" + table.getName() + ".xlsx");
        workbook.write(out);
        workbook.close();
    }

}
